<template>
  <div>
    <div class="view-login">
      <div class="login-content">
        <el-row :gutter="0">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <div class="left" style="z-index: 0">
              <el-image :src="require('../../../static/bg/login.png')" style="width: 79%; height: 100%">
                <div slot="placeholder" class="image-slot">
                  加载中<span class="dot">...</span>
                </div>
              </el-image>
            </div>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <div class="right" style="z-index: 1">
              <div class="right-content">
                <h2 class="title">
                  <span>账号登录</span>
                </h2>
                <el-tabs v-model="activeName" @tab-click="handleClick" style="width: 42%;margin: 0 auto">
                  <el-tab-pane label="账号登录" name="0"></el-tab-pane>
                  <el-tab-pane label="手机短信登录" name="1"></el-tab-pane>
                </el-tabs>
                <div class="login-tab" v-if="currentIndex === 0">
                  <el-form
                    ref="loginForm"
                    :model="loginForm"
                    :rules="rules"
                    label-width="0"
                    style="background-color: #fff">
                    <el-form-item prop="username">
                      <el-input v-model="loginForm.username" placeholder="请输入邮箱/手机号/账号" clearable></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                      <el-input type="password" v-model="loginForm.password" autocomplete="off"
                                placeholder="请输入密码" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                      <div class="flex-between">
                        <el-checkbox v-model="loginForm.remember">下次自动登录</el-checkbox>
                        <el-button type="text" style="color: #74787c">忘记密码？</el-button>
                      </div>
                    </el-form-item>
                  </el-form>
                </div>
                <div class="login-tab" v-if="currentIndex === 1">
                  <el-form
                    ref="loginForm"
                    :model="loginForm"
                    :rules="rules"
                    label-width="0"
                    style="background-color: #fff">
                    <el-form-item prop="username">
                      <el-input v-model="loginForm.username" placeholder="请输入手机号" clearable></el-input>
                    </el-form-item>
                    <el-form-item prop="verificationCode">
                      <el-input v-model="verificationCode" placeholder="请输入验证码" clearable
                                style="width: 313px"></el-input>
                      <el-button type="primary">发送验证码</el-button>
                    </el-form-item>
                    <el-form-item>
                      <div class="flex-between">
                        <el-checkbox v-model="loginForm.remember">下次自动登录</el-checkbox>
                        <el-button type="text" style="color: #74787c">忘记密码？</el-button>
                      </div>
                    </el-form-item>
                  </el-form>
                </div>
                <BottomBox @login="goLogin" @register="goRegister"></BottomBox>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>

import BottomBox from '../../components/BottomBox'
export default {
  name: 'Login',
  components: {BottomBox},
  data () {
    return {
      activeName: '0',
      loginForm: {
        account: '',
        email: '',
        username: '',
        // 任选一个传到后端
        password: '',
        remember: false
      },
      currentIndex: 0,
      verificationCode: '',
      rules: {
        username: [
          {required: true, message: '请输入邮箱/手机号/账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleClick (tab, event) {
      this.currentIndex = tab.name === '0' ? 0 : 1
    },
    goLogin () {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          console.log('userInfo:', this.loginForm)
          this.$store.dispatch('submitLogin', this.loginForm).then(res => {
            if (res.code === 200) {
              this.resetForm()
              this.$router.push('/MainPage')
            } else {
              this.$router.push('/')
            }
          })
        } else {
          return false
        }
      })
    },
    goRegister () {
      this.$router.push('/Register')
      this.resetForm()
    },
    resetForm () {
      this.$refs['loginForm'].resetFields()
    }
  }
}
</script>

<style scoped>
.view-login {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background: #f8f9fa;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.view-login .el-button, .view-login .el-input__inner {
  height: 48px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.view-regist .login-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.view-login .login-content {
  display: flex;
  justify-content: space-between;
}

.view-login .login-content .left {
  margin-right: -100px;
}

.view-login .login-content .left img {
  display: block;
  height: 538px;
}

.view-login .login-content .right .right-content {
  width: 431px;
  padding: 0 35px;
  background: #fff;
  border: 1px solid #eee;
  box-shadow: 0 0 61px 0 rgb(85 108 144 / 7%);
}

.view-login .login-content .right .title {
  position: relative;
  padding: 18px 0;
  text-align: center;
}

.view-login .login-content .right .title span {
  font-size: 22px;
  font-weight: 700;
}

.view-login .login-content .right .right-content .tabs-nav {
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
}

.view-login .login-content .right .right-content .tabs-nav .item {
  cursor: pointer;
  position: relative;
  padding-bottom: 9px;
  font-size: 18px;
  font-family: PingFangSC, PingFangSC-Regular;
  font-weight: 400;
  text-align: left;
  color: #74787c;
  line-height: 32px;
}

.login-tab .flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
